#ifndef TCPNET_H
#define TCPNET_H
#include <map>
#include <list>
#include <functional>
#include <stdint.h>
#include <string>
#include <boost/function.hpp>

using namespace std;

#define HOST_CONNECT_FAILED "Connect Failed!"



//typedef std::map<std::string, std::string>	DataMap;
typedef boost::function< bool ( const std::string &) > NetCallback;
struct NetObj{
    NetObj(NetCallback callback, std::string & sendMsg):
        callback_(callback)
        ,sendmsg_(sendMsg){

        }
    NetCallback callback_;
    std::string sendmsg_;
};

class Host{
    struct Impl;
    Impl *impl_;

    //void mainThreadRun();
    friend class TcpNet;
    Host();
    ~Host();
public:
    void setAddress(const std::string& ip);
    std::string &address();
    void addAsyncRequest( NetCallback callback, std::string& data);
    void addListener( NetCallback callback);
    void connInit();
    bool isConn();
    void close();
};

namespace boost{
    namespace asio{
    class io_service;
    };
};

class TcpNet
{

    static  TcpNet* inst;
    TcpNet();


    struct Impl;
    Impl* impl_;
    friend class Host;
    int port_;

public:
    ~TcpNet();
    static TcpNet *instance();
    void startThread();
    virtual void runMain();
    void stop();

    void threadRun();

    void setPort(int port){ port_ = port;}
    bool hasHost(const std::string &ip);
    Host* getHost(const  std::string &ip);
    Host* createHost(const  std::string &ip);//return the one found if already there
    bool removeHost(const  std::string &ip);

    boost::asio::io_service * getMainService();
    boost::asio::io_service * getScoketService();
};




#endif // TCPNET_H
